<?php
namespace App\Http\Controllers\Admin;

use App\Models\Role;
use App\Models\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Models\Group;
use Illuminate\Support\Facades\DB;
/**
 *
 * @author Joker
 *
 */
class RolesController extends BaseController
{

    public function index()
    {
        if(!$this->user->can('viewSettingRole')) abort(405);
        
        $roles = Role::paginate(20);
        return view('admin.setting.roles.index')->with('roles',$roles);
    }
    
    public function create()
    {
        if(!$this->user->can('editSettingRole')) abort(405);
        $group = Group::all();
        $topPerm = Permission::with('childrens')->where('pid','=',0)->get();
        return view('admin.setting.roles.details')->with('topPerm',$topPerm)->with('group',$group);
    }
    
    public function edit($id)
    {
        if(!$this->user->can('viewSettingRole')) abort(405);
        
        $result = Role::find($id);
        $perms = array_fetch($result->perms->toArray(),'id');
        $group = Group::all();
        $group_id = DB::table('group_role')->where('role_id','=',$id)->pluck('group_id');
        $topPerm = Permission::with('childrens')->where('pid','=',0)->get();
        return view('admin.setting.roles.details')->with('topPerm',$topPerm)->with('result',$result)->with('perms',$perms)->with('group',$group)->with('group_id',$group_id);
    }
    
    public function store(Request $req)
    {
        if(!$this->user->can('editSettingRole')) 
            return $this->formatJson('error',$this->permerror);
        
        $perms = Permission::all();
        $data = array();
        foreach($perms as $pm)
        {
            $data[] = $req->input($pm->name,0);
        }
        $roleid = $req->input('roleid',0);
        $rolename = $req->input('name',0);
        $display_name = $req->input('display_name','');
        $description = $req->input('description','');
        $groupid = $req->input('group',0);
        if(!empty($roleid) && is_numeric($roleid) &&$roleid > 0)
        {
            $role = Role::find($roleid);
            $role->name = $rolename;
            $role->display_name = $display_name;
            $role->description = $description;
            $role->save();
            
        }
        else 
        {
//            $insert_row = array(
//                'name'           => $rolename,
//                'display_name'   => $display_name,
//                'description'    => $description
//            );
//            $role = Role::firstOrCreate($insert_row);
            $role = new Role();
            $role->name = $rolename;
            $role->display_name = $display_name;
            $role->description = $description;
            $role->save();
        }
        $hasGroup = DB::table('group_role')->where('role_id',$role->id)->first();
        if(!empty($hasGroup))
        {
            if($hasGroup->group_id != $groupid)
            {
                DB::table('group_role')->where('role_id',$role->id)->delete();
                DB::table('group_role')->insert(['group_id'=>$groupid,'role_id'=>$role->id]);
            }
        }
        else
        {
            DB::table('group_role')->insert(['group_id'=>$groupid,'role_id'=>$role->id]);
        }
        
        $data = array_where($data, function($key, $value){
                    return $value > 0;
                });
        if(!empty($data))
        {
            $role->perms()->sync($data);
        }
        
        return Response::json(array('id'=>$role->id,'status'=>'success','msg'=>'角色信息及权限保存成功！','redirectUrl'=>'/admin/setting/roles'));
    }
    
    public function destroy($id)
    {
        if(!$this->user->can('deleteSettingRole'))
            return $this->formatJson('error',$this->permerror);
        $role = Role::find($id);
        if(empty($role))
        {
            return $this->formatJson('error','资源不存在！');
        }
        if($role->delete())
            return  $this->formatJson('success','角色组：［'.$role->name.'］ 删除成功');
    }
}